caboCity.implement({
	initVarsHouses: function(){
		this.hcounter = 0;
	
		this.houseZones = [] //ALL house zones.
		this.house = []; //Each house (main house of a group of tiles)
		
		this.totalFreeSpace = 0; //Total free space in the city
		this.hWithFreeSpace = []; //All houses with free space
	}
	
	//Add a new house zone
	, addNewHouseZone: function(coorIni, coorEnd){
		$$('.shadow').destroy();
		for(var i=coorIni.x; i<=coorEnd.x; i++){
			for(var j=coorIni.y; j<=coorEnd.y; j++){
				if( this.isTileBuildable({x: i, y: j}) )
					this.addNewHouseZoneTile({x: i, y: j});
			}
		}
	}
	
	,addHouseZoneShadow: function(coorIni, coorEnd){
		$$('.shadow').destroy();
		for(var i=coorIni.x; i<=coorEnd.x; i++){
			for(var j=coorIni.y; j<=coorEnd.y; j++){
				if( this.isTileBuildable({x: i, y: j}) )
					this.addHouseZoneTileShadow({x: i, y: j});
			}
		}
	}
	//Add single house tile
	,addHouseZoneTileShadow: function(coor){
		if (!this.isTileBuildable(coor) ) return false;
		var pos = this.p(coor);
		var el = new Element('div', {
			styles : {
				left: pos.x,
				top	: pos.y
			}
		});
		el.addClass('isopers').addClass('shadow').addClass(d.e.c.houseZone.hz).addClass(d.e.c.houseZone.shadow);
		el.inject(this.cont);
	}
	
	//Add new house zone tile
	, addNewHouseZoneTile: function(coor){
		if (!this.isTileBuildable(coor) ) return false;
		
		this.takeThisTile(coor, d.tt.houseZoneTile);
		var hz = new cHouse(coor);
		var pos = this.p(coor);
		
		//Create house zone element
		hz.el = new Element('div', {
			text: "0"
			,styles : {
				left: pos.x,
				top: pos.y
			}
		});
		hz.el.addClass('isopers').addClass(d.e.c.houseZone.hz).addClass(d.e.c.houseZone.hznormal);
		hz.el.inject(this.cont);
		
		var v = this.coor2vec(coor);
		this.houseZones[v] = hz;
		this.house[v] = hz;
		this.hWithFreeSpace[v] = hz;
	}
	
	//Remove TILE of a house
	,removeHouse: function(coor){
		var v = this.coor2vec(coor);
		var hz = this.houseZones[v];
		if(!hz) return;
		hz.remove();
		this.houseZones[v] = null;
		this.house[v] = null;
		this.hWithFreeSpace[v] = null;
		this.freeThisTile(coor);
	}
	
	, getNearHousesFrom: function(coor){
		
	}
	,isHouse: function(coor){
		if(this.house[ this.coor2vec(coor) ]) return true;
		else return false;
	}
	,getHouse: function(coorH){
		var h = this.house[ this.coor2vec(coorH) ];
		return h.getMainTile();
	}
	,toHouseFrom: function(from, hcoor){
		if(!this.isHouse(hcoor)) return null;
		
		h = this.house[ this.coor2vec(hcoor) ];
		var roadList = this.getRoadsNearElement(h.coor, h.size, true); //Devuelve null :O! **CORREGIR!
		
		var pl = null;
		//var housePathLists = new Array();
		roadList.each(function(rc){
			var currpl = /*housePathLists.include(*/this.Dijkstra(from, rc)/*)*/;
			if(!pl || pl.length > currpl.length)
				pl = currpl;
		}.bind(this));
		
		/*
		Randomized
		var rand = Math.floor(Math.random()*housePathLists.length);
		var pl = housePathLists[ rand ];*/
		
		return pl;
	}
});

var houseAtrib = new Class({
	initialize: function(){
		newCaboCity.totalFreeSpace += 4;
		this.level = 0;
		this.people = 0;
		this.capacity = 4;
		this.food = 0;
		this.water = 0;
	}
});

//@coor := tile coor
//@size := size in tiles
//@tiles := related tiles (by default itself)
//@mainTile := Stores the main tile of the "the house".
//@atrb := Store all the information of the house. Its useful atributes, etc.
//@el := Store the html element of the house when this tile is the main tile.
var cHouse = new Class({
	initialize: function(coor){
		this.coor = coor;
		this.size = {x: 1, y: 1}; //1 tile size
		this.tiles = [].include(coor);
		this.mainTile = coor;
		this.atrb = new houseAtrib;
		this.el = null;
		
		newCaboCity.hWithFreeSpace[ newCaboCity.coor2vec[this.coor] ] = this.mainTile;
		
		this.checkID = this.check.periodical(3000, this); //Check thinkgs (is road near, etc...)
	}
	,getMainTile: function(){
		return newCaboCity.house[ newCaboCity.coor2vec(this.mainTile) ];
	}
	/*
	,removeTile: function(tile){
		var tmpTiles = this.tiles;
		this.tiles = new Array();
		
		var noMT = false;
		tmpTiles.each(function(c){
			if(c.x==tile.x && c.y==tile.y) {
				noMT = true;
				return;
			}
			this.tiles.include(c);
		});
		
		//Assign new main tile
		if(noMT) {
			var mx = infinite;
			var my = infinite;
			this.tiles.each(function(c){
				if(mx > )
			});
		}
	}*/
	,associateTile: function(newTile){
		this.tiles.include(newTile);
	}
	
	//Updagrade
	,upgrade: function(){ //Upgrade to next level
		if(this.mainTile.x==this.coor.x && this.mainTile.y==this.coor.y){
			switch(this.atrb.level){
				case 0:
					this.el.set("text", "1")
					this.atrb.level = 1;
			}
		} else {
			var masterHouse = this.houseZones[coor2vec(this.mainHouse)]
			masterHouse.update();
		}
	}
	//Decay the house
	,decay: function(){ //Decay one level
	
	}
	,check: function(){
		if(!newCaboCity.isRoadNearFromElement(this.coor, this.size, true) ) this.noRoadNear();
	}
	,noRoadNear: function(){
		newCaboCity.removeHouse(this.coor);
	}
	,remove: function(){
		this.el.destroy();
		clearInterval(this.checkID);
	}
});

//Inmigration related actions
//This IS mater house (the main tile)
cHouse.implement({
	a_ringring: function(newPeople){
		if(this.atrb.people < this.atrb.capacity && newPeople <= (this.atrb.capacity-this.atrb.people)){
			this.atrb.people += newPeople;
			if(this.atrb.level == 0){ //If there are no house yet
				this.upgrade(); //construct new house!
			}
			
			newCaboCity.population += newPeople;
			newCaboCity.totalFreeSpace -= newPeople;
			if(this.atrb.people != this.atrb.capacity) {
				var p = newCaboCity.coor2vec(this.coor);
				newCaboCity.hWithFreeSpace[p] = this;
			}
			return true;
		} else return false;
	}
});
